import request from '@/utils/request';
import { AxiosPromise } from 'axios';
// import {  } from '@/api/front/user/types';

/**
 * 获取用户个人信息
 * @param query 
 * @returns 
 */
export const userInfo = (query?: any): AxiosPromise<any> => {
  return request({
    url: '/api/user/info',
    method: 'get',
    params: query
  });
};

/**
 * 修改用户信息
 * @param query 
 * @returns 
 */
export const userModifyInfo = (query?: any): AxiosPromise<any> => {
  return request({
    url: '/api/user/modify/info',
    method: 'post',
    data: query
  });
};

/**
 * 检查用户是否存在
 * @param username 用户名
 * @returns boolean
 */
export const existUser = (username: string): AxiosPromise<any> => {
  return request({
    url: '/api/user/exist/' + username,
    method: 'post'
  });
};

/**
 * 修改密码 
 * @param formData {username: string, password: string}
 * @returns 
 */
export const updatePassword = (formData: any): AxiosPromise<any> => {
  return request({
    url: '/api/user/update/password',
    method: 'post',
    data: formData
  });
};

export const updateUserInfo = (formData: any): AxiosPromise<any> => {
  return request({
    url: '/api/user/modify/info',
    method: 'post',
    data: formData
  });
};

/**
 * 发送短信验证码
 * @param phonenumber 手机号
 * @returns 
 */
export const sendSmsCaptcha = (phonenumber: string): AxiosPromise<any> => {
  return request({
    url: '/resource/sms/code',
    method: 'get',
    params: { phonenumber }
  });
};

/**
 * 验证短信验证码
 * @param phonenumber 手机号
 * @param code 验证码
 * @returns boolean
 */
export const verifySmsCaptcha = (phonenumber: string, code: string): AxiosPromise<any> => {
  return request({
    url: '/resource/sms/code/check',
    method: 'post',
    data: { username: phonenumber, code }
  });
};

export const sendEmailCaptcha = (email: string): AxiosPromise<any> => {
  return request({
    url: '/resource/email/code',
    method: 'get',
    params: { email }
  });
};

/**
 * 绑定邮箱
 * @param email 邮箱地址
 * @param code 验证码
 */
export const bindEmail = (data: { email: string; emailCode: string }): AxiosPromise<any> => {
  return request({
    url: '/api/user/bind/email',
    method: 'post',
    data
  });
};

/**
 * 用户头像上传
 * @param data 头像文件
 */
export const uploadAvatar = (data: FormData) => {
  return request({
    url: '/api/user/avatar',
    method: 'post',
    data: data
  });
};

/**
 * 获取用户信息
 * @param data 头像文件
 */
export const getUserProfile = (data: any, headers: any) => {
  return request({
    url: '/api/user/profile',
    method: 'post',
    data: data,
    headers: headers
  });
};
